package eco2automotive.tasks;

import java.util.*;
import eco2automotive.port.ConnectionControl;
import eco2automotive.util.Clock;
import eco2automotive.data.HashParameters;
import eco2automotive.data.Parameter;
import eco2automotive.gui.Layout_MONITORING;
/**  
 *  Thread - Send and receive data relative to PIDs (see eco2utomotive/data/StandarPIDs.xml)
 *  
  * @version 1.0
  * @autor Fco. Javier Sánchez Esparza
 *  @see eco2automotive.data.HashParameters
 *  @see eco2automotive.port.ConnectionControl
  *
  */ 
public class Thread_MONITOR extends Thread{
    ConnectionControl connControl;
    Layout_MONITORING LAYOUT;
    private boolean next = true; 
    HashParameters h;       
    Clock wait;
   /**
     * Start the thread and @param connControl send the data from @h HashParameter
     * @param connControl connection active object
     * @param LAYOUT JDialog used to display the monitor
     * @param h HashMap that contains the information to send.
     */ 
  public Thread_MONITOR(ConnectionControl connControl, Layout_MONITORING LAYOUT, HashParameters h)
    {
       this.h=h;
        this.connControl=connControl; 
        this.LAYOUT=LAYOUT;
        
    }
    
  /**
   * Stop the thread
   */
  public void stop_Thread()
        {
              next=false;
              wait.stopTimer();
         }
         public void run()
   {                  
       Iterator it = h.getHash().entrySet().iterator();
       wait=new Clock(3);
      while (next)
      {               
        if (connControl.DATA_AVAILABLE()==false&&wait.getFlag()==true)
        {                           
            if (it.hasNext()) {                 
                                    Map.Entry e = (Map.Entry)it.next();
                                    Parameter p = (Parameter)e.getValue();
                                    
//Send MODE 03 and 04
                if((p.getOperationMode().contains("03")!=true))
                        if((p.getOperationMode().contains("04")!=true))                                                    
                        {                    
                             System.out.println("Send... "+p.getOperationMode()+p.getPidKey());
                               connControl.TXData(p.getOperationMode()+p.getPidKey());                                                  
                               try{Thread.sleep(200);}catch(Exception ex){}
                               //200ms
                               
                               this.LAYOUT.updatePrgogress(p.getOperationMode()+p.getPidKey());
                        }
                else{//
                    System.out.println("Mode:"+p.getOperationMode());
                    System.out.println("PID:"+p.getPidKey());                    
                        }                                                        
                        }
            else{it = h.getHash().entrySet().iterator();}
            
        }        
        else if(connControl.DATA_AVAILABLE()==true){
        String data;                
        
        for(int i=0;i<=connControl.getFata().getLength();i++){//bUFFER OF DATA 
                       
            String str= new String(connControl.getFata().extract().toString());            
             this.LAYOUT.updateData(str);                                    
        }
        this.connControl.downFlag();               
        
        }
   
      }
      
   } 
         
}
